/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConsultaProjetosView.java
 *
 * Created on 20/06/2011, 12:36:10
 */

package calculadorfpa.applications.consultaprojeto;

import calculadorfpa.CalculadorFPAView;
import calculadorfpa.applications.novoprojeto.NovoProjetoView;
import calculadorfpa.applications.projetomelhoria.resources.ProjetoMelhoriaView;
import calculadorfpa.connection.DBUtils;
import calculadorfpa.modelo.AbstratoProjeto;
import calculadorfpa.modelo.AbstratoProjetoCA;
import calculadorfpa.modelo.Projeto;
import calculadorfpa.modelo.ProjetoCAEstimativa;
import calculadorfpa.modelo.ProjetoCAIndicativa;
import calculadorfpa.modelo.ProjetoMelhoria;
import calculadorfpa.persistence.Persistence;
import calculadorfpa.tablemodels.TableModelConsultaProjetos;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author TECBMEPI
 */
public class ConsultaProjetosView extends javax.swing.JPanel {

    private List<Projeto> listaFiltro = null;
    
    /** Creates new form ConsultaProjetosView */
    public ConsultaProjetosView() {
        CalculadorFPAView.frameView.setTitle("Visualizar Projetos Salvos");
        initComponents();
        popularTabela();
    }

    private void popularTabela() {
        tableProjetos.setModel(new TableModelConsultaProjetos());
        listaFiltro = CalculadorFPAView.listaProjetos;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        btnCriarProjeto = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableProjetos = new javax.swing.JTable();
        btnExcluir = new javax.swing.JButton();
        btnAlterar = new javax.swing.JButton();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(calculadorfpa.CalculadorFPAApp.class).getContext().getResourceMap(ConsultaProjetosView.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        btnCriarProjeto.setText(resourceMap.getString("btnCriarProjeto.text")); // NOI18N
        btnCriarProjeto.setName("btnCriarProjeto"); // NOI18N
        btnCriarProjeto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCriarProjetoActionPerformed(evt);
            }
        });

        jSeparator1.setName("jSeparator1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tableProjetos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tableProjetos.setName("tableProjetos"); // NOI18N
        jScrollPane1.setViewportView(tableProjetos);

        btnExcluir.setText(resourceMap.getString("btnExcluir.text")); // NOI18N
        btnExcluir.setName("btnExcluir"); // NOI18N
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        btnAlterar.setText(resourceMap.getString("btnAlterar.text")); // NOI18N
        btnAlterar.setName("btnAlterar"); // NOI18N
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCriarProjeto, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnAlterar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnExcluir)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCriarProjeto)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 359, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnExcluir)
                    .addComponent(btnAlterar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnCriarProjetoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCriarProjetoActionPerformed
        CalculadorFPAView.frameView.showApp(new NovoProjetoView(null));
    }//GEN-LAST:event_btnCriarProjetoActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        int index = tableProjetos.getSelectedRow();
        
        if(index < 0) {
            JOptionPane.showMessageDialog(this, "Selecione um projeto para exclusão", "Alerta", JOptionPane.WARNING_MESSAGE);
            return;
        }
        
        AbstratoProjeto projeto = listaFiltro.get(index);
        try {
            System.out.println(projeto.getId());
            Persistence.exluirProjeto(projeto.getId());
            CalculadorFPAView.listaProjetos = Persistence.consultaProjetos();
            CalculadorFPAView.frameView.showApp(new ConsultaProjetosView());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnExcluirActionPerformed

    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarActionPerformed
        int index = tableProjetos.getSelectedRow();

        if(index < 0) {
            JOptionPane.showMessageDialog(this, "Selecione um projeto para Ver Informações/Alterar", "Alerta", JOptionPane.WARNING_MESSAGE);
            return;
        }

        AbstratoProjeto projeto = listaFiltro.get(index);


        if(projeto.getClass() == Projeto.class) {
            CalculadorFPAView.frameView.showApp(new NovoProjetoView((Projeto)projeto));
        } else if (AbstratoProjetoCA.class.isAssignableFrom(projeto.getClass())) {
        }
}//GEN-LAST:event_btnAlterarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAlterar;
    private javax.swing.JButton btnCriarProjeto;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable tableProjetos;
    // End of variables declaration//GEN-END:variables

}
